<?php

declare(ENCODING = 'UTF-8');

/*                                                                        *
 * This script is part of the TYPO3 project - inspiring people to share!  *
 *                                                                        *
 * TYPO3 is free software; you can redistribute it and/or modify it under *
 * the terms of the GNU General Public License version 2 as published by  *
 * the Free Software Foundation.                                          *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *	
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General      *
 * Public License for more details.                                       *
 *                                                                        */	

/**
 * @package auxo
 * @subpackage Logging
 * @version $Id$
 */

/**	
 * The TimelyRollingFileAppender
 *	
 * PeriodRollingFileAppender extends {@link tx_auxo_LogFileAppender} so that 
 * the underlying file is rolled over at a user chosen frequency.
 *
 * <p>The rolling schedule is specified by the <b>DatePattern</b>
 * option. This pattern should follow the conventions of php function 
 * date() which is used. A formatted version of the date pattern is used 
 * as the suffix for the rolled file name. It must be therefore a valid 
 * os filename.
 *
 * <p>For example, if the <b>File</b> option is set to
 * <code>/foo/bar.log</code> and the <b>DatePattern</b> set to
 * <code>.Y-m-d</code>, on 2001-02-16 at midnight, the logging
 * file <code>/foo/bar.log</code> will be copied to
 * <code>/foo/bar.log.2001-02-16</code> and logging for 2001-02-17
 * will continue in <code>/foo/bar.log</code> until it rolls over
 * the next day. 
 * 
 * @package auxo
 * @subpackage Logging
 * @version $Id$	
 * @copyright Copyright belongs to the respective authors
 * @author AHN
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License, version 2
 */
class  tx_auxo_LogTimelyRollingFileAppender extends tx_auxo_LogFileAppender {
	
	/*
	 * Date pattern that is used to format a date.
	 * 
	 * @var $datePattern string
	 */
	private $datePattern;

	/**
	 * Creates a new TimelyRollingFileAppender
	 * 
	 * This constructor requires a log layout, a log filename and a date pattern. If no date 
	 * pattern is supplied 'w.Y' is used means week number and 4 digit year.
	 * For more details about $datepattern see {@link tx_auxo_LogDailyRollingFileAppender}
	 *
	 * @param tx_auxo_LogLayoutInterface $layout layout that is used to log events
	 * @param string $filename filename to log events to
	 * @param string $datePattern a date pattern based on php function date()
	 */
	public function __construct($layout, $filename, $datePattern='w.Y') {
		$this->datePattern = $datePattern;
		$this->filename = $filename;
		
		if (is_readable($filename)) {
			$this->rolloverIfNeeded();			
		}
		
		parent::__construct($layout, $filename);
	}
	
	/**
	 * Appends an event to this appender
	 *
	 * @param tx_auxo_LogEvent $event
	 * @return void
	 */
	public function append($event) {
		if ($this->rolloverIfNeeded()) {
			$this->openFile($this->filename);
		}
		parent::append($event);
	}
	
	/**
	 * Checks wether a log files needs to be rolled over due to 
	 * a given date pattern
	 *
	 * @return boolean $rolledover returns true if $filename has been rolledover
	 */
	protected function rolloverIfNeeded() {
		clearstatcache();
		$fileLastChanged = date($this->datePattern, filemtime($this->filename));
		$now = date($this->datePattern, now());
		
		if ($now == $fileLastChanged) {
			return false;
		}
		
		if ($this->handle) {
			$this->close();
		}
		
		$target = $this->filename . '.' . $fileLastChanged;
		if (is_readable($target)) {
			if (! unlink($target)) {
				throw new tx_auxo_IOException(sprintf('Can not remove target file %s', $target));				
			}
		}
		
		if (! rename($this->filename, $target)) {
			throw new tx_auxo_IOException(sprintf('Logfile %s renaming to %s failed', $this->filename, $target));
		}
		return true;
	}
}
?>